import React, { PureComponent, Fragment } from 'react';
import { Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { Switch, Route, withRouter } from 'react-router';
import PayRoutes, { basePath as payPath } from './pay';
import EscortRoutes, { basePath as escortPath } from './escort';

import AppRoutes, { basePath as appPath } from './app';


import Loading from '../components/common/loading';
import NotFound from '../pages/app/404';

import { save, get } from '../utils/storage';
import { getQueryParam } from '../utils/url';
import { COMMIT } from '../constants/setting';
import toast from '../utils/toast';


// 所有需要loading的effects
const effectsNeedLoading = [
  'pay/initData',
  'code/preFetchCouponQRStatus',
  'invoice/fetchUrl',
  'invoice/addOrEditInfo',
  'invoice/delInfo',
];

@withRouter
@connect((state) => {
  const { loading } = state;
  return {
    showLoading: effectsNeedLoading.some(effect => loading.effects[effect]),
  };
})
class IndexRoutes extends PureComponent {
  componentDidMount() {
    this.checkVersion();
    const openId = getQueryParam('openId');
    if (openId) {
      save('openId', openId);
    }
    console.log('=++++++', window.source);
    if (window.source) {
      save('source', window.source);
    } else {
      window.source = get('source');
    }
  }

  componentDidCatch(error, info) {
    console.error(error, info);
  }

  checkVersion = () => {
    const script = document.createElement('script');
    script.type = 'text/javascript';
    script.src = `/version_file.js?v=${Date.now()}`;
    script.onload = () => {
      console.log(`本地${COMMIT},服务器${window.git_commit}`);
      if (window.git_commit && COMMIT !== window.git_commit) {
        toast('与服务器代码不匹配，自动更新页面');
        const oldUrl = window.location.href;
        window.location.href = oldUrl.split('#')[1].indexOf('?') === -1 ? `${oldUrl}?update_v=${Date.now()}` : `${oldUrl}&update_v=${Date.now()}`;
      }
    };
    document.getElementsByTagName('head')[0].appendChild(script);
  }

  render() {
    const yyeCode = getQueryParam('yyeCode');
    if (yyeCode && ['/app/loginWithCode', '/login'].every(path => this.props.location.pathname.indexOf(path) === -1)) {
      return (
        <Redirect
          to={{
            pathname: '/app/loginWithCode',
            search: this.props.location.search,
            state: { redirect: this.props.location.pathname },
          }}
        />
      );
    }

    const { showLoading } = this.props;
    let loading;
    if (showLoading) {
      loading = <Loading />;
    }
    return (
      <Fragment>
        <Switch>
          <Route key="pay" path={payPath} component={PayRoutes} />
          {/* <Route key="station" path={stationPath} component={StationRoutes} />
          <Route key="cq" path={cqPath} component={CqRoutes} />
          <Route key="qiji" path={qijiPath} component={QijiRoutes} /> */}
          <Route key="escort" path={escortPath} component={EscortRoutes} />
          {/* <Route key="tmall" path={tmallPath} component={TmallRoutes} /> */}
          <Route key="app" path={appPath} component={AppRoutes} />
          {/* <Route key="wap" path={wapPath} component={WapRoutes} /> */}
          <Route component={NotFound} />
        </Switch>
        {loading}
      </Fragment>
    );
  }
}

export default IndexRoutes;

